<?php

//request vars
$requestedService = $_REQUEST["service"];
$suppliedZipCode = $_REQUEST["zipCode"];
$suppliedSiteId = $_REQUEST["suppliedSiteId"];
$foodBankServiceTypeID = $_REQUEST["foodBankServiceTypeID"];
////var for logging logSearch( $logZip, $logSite, $logTime, $logIp )
$logZip = $_REQUEST["logZip"];
$logSite = $_REQUEST["logSite"];
$logTime = $_REQUEST["logTime"];
$logIp = $_REQUEST["logIp"];


//service names
$PROXIMITY_SEARCH_BY_ZIP = "proximitySearchByZip";
$LIST_FOOD_BANK_SERVICE_TYPES = "listFoodBankServiceTypes";
$LOG_SEARCH_PARAMS = "logSearchParams";
$GET_SITE_DETAILS = "getSiteDetails";





//runtime parameters
$objectTypeRequested = "";

//service includes
include( "resources/BasicXMLNode.php" );
include( "resources/LogSiteSearch.php" );
require('classes/Config.inc.php');
require('classes/DbConnect.class.php');

//resource includes
include( "services/FoodBankProximitySearch.php" );
include( "services/ListFoodBankServiceTypes.php" );

//connect to db
function connect()
{
	//Set up all paramaters for the connection
	$db = new DbConnect();
	  
	//Open the connection to the database
	$db->open() or die($db->error());
}

//call requested service
function callService( $name )
{
	switch( $name ){
		//
		case $PROXIMITY_SEARCH_BY_ZIP:
		connect();
		$objectTypeRequested = "site";
		$siteResults = sitesByZipAndService( $suppliedZipCode, $foodBankServiceTypeID );
		$numberOfResults = count( $siteResults );
		logSearch( $logZip, $numberOfResults, $logTime, $logIp );
		return $siteResults;
		//
		case $LIST_FOOD_BANK_SERVICE_TYPES:
		connect();
		$objectTypeRequested = "serviceType";
		return getFBServiceTypes();
		//
		case $LOG_SEARCH_PARAMS:
		die( "<root><ok>" . logSelected( $logZip, $logSite, $logTime, $logIp ) . "</ok></root>" );
		//get details
		case $GET_SITE_DETAILS:
		connect();
		return getSiteDetails( $suppliedSiteId );
	}
}

$result = callService( $requestedService );

if ($result)
{

	//return xml
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<root>";
	while ($dataRow = mysql_fetch_assoc($result)) {
		
		echo basicXMLNode( $dataRow, $objectTypeRequested );
		
	}
	echo "</root>";
}
else echo "No Results";

?>